package cn.edu.cqu.fredyvia.forum.Utils;

import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotBlank;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class Utils {
    public static String getCurTime() {
        return Long.toString(System.currentTimeMillis());
    }

    public static String getUUID() {
        String uuid = java.util.UUID.randomUUID().toString();
        return uuid;
    }

    public static boolean isBlank(String s){
        return s==null || s.trim().equals("");
    }

    public static String getRandomFileName() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(getCurTime());
        stringBuilder.append("-");
        stringBuilder.append(getUUID().replaceAll("-", ""));
        return stringBuilder.toString();
    }

    public static String getRandomFileName(@NotBlank String suffix) {
        StringBuilder stringBuilder = new StringBuilder(getRandomFileName());
        stringBuilder.append(".");
        stringBuilder.append(suffix);
        return stringBuilder.toString();
    }

    public static String getSuffix(String fileName) throws Exception {
        //System.out.println(fileName.split("\\.")[1]);
        // return "jpg";
        String regix = "[^\\.]\\w*$";
        Pattern r = Pattern.compile(regix);
        Matcher m = r.matcher(fileName);
        if (m.find( )) {
            log.info("Found value: " + m.group(0));
            return m.group(0);
        }
        throw new Exception("suffix not found");
    }
}
